iT邦幫忙

2021 iThome 鐵人賽

DAY 21
0
Software Development

Redis還在學系列 第 21

Day21 Redis架構實戰-高可用性

  • 分享至 

  • xImage
  •  

Redis高可用性

  • 前面的說明與範例都是透過單台的Redis Server的方式進行,這樣的配置下當節點出現故障時,等於整個Redis的服務就會停止,此時會影響到用戶端的相關操作,重則讓整個應用也跟著停止服務.高可用性的架構就是要避免單一節點的錯誤造成服務中斷,透過資料冗餘從Master同步複製到Replica和當失效的時候可以透過機制切換,避免服務的中斷.
# 高可用配置
                           --->   Redis (Replica)
client ---> Redis (Master)
                           --->   Redis (Replica)

# 當Master掛掉時,會把Replica切換為Master提供用戶端服務


Client ----------------------->   Redis (Master)
Client                                  | 同步
            Redis (X)                   V
                                  Redis (Replica)
  • 讀寫分離
    • 當用戶端需要讀寫的時候,必須要透過Master才可以進行讀寫操作.
    • 當用戶端只需要讀取資料而不需要寫入的時後,可以考慮直接連Replica取得資料,減輕Master的負擔.

            讀取/寫入
Client ----------------------->   Redis (Master)
                                        | 同步
                                        V
Client ----------------------->   Redis (Replica)
            讀取only


  • 常見高可用架構配置
# 高可用配置
                           --->   Redis (Replica)
client ---> Redis (Master)
                           --->   Redis (Replica)


# 高可用鏈結配置(應用於分散多個資料中心架構或是允許較慢同步的需求)

client ---> Redis (Master) ---> Redis (Replica) ---> Redis (Replica)
            _____________________      _________ 
                        A區資料中心 (LAN)        (WAN)    B區資料中心
                                            or
                        應用服務                           報表服務
  • 實際配置

在配置Redis Server高可用架構時,基本上會配置三台伺服器,並給予相同或是不同的Port來配置,下面實際配置的內容會在單機進行,故會將Port分配三個來模擬三台伺服器的狀況.

因需要在同一台機器模擬三台伺服器的配置,故有以下前置作業需要先完成 (如果三台伺服器則不需要這樣做)

  • 6379-->Reids Server (Master)
  • 6380-->Reids Server (Replica)
  • 6381-->Reids Server (Replica)
# 複製三份設定檔,結尾用Port命名
$ cp /home/redis/config/redis.conf redis_6379.conf
$ cp /home/redis/config/redis.conf redis_6380.conf
$ sudo cp /home/redis/config/redis.conf redis_6381.conf

# 修改各個設定檔中的rdb與aof檔案名稱,一樣結尾用Port命名
# redis_6379.conf
vi dump_6379.rdb
dbfilename dump_6379.rdb
appendfilename "appendonly_6379.aof"

# redis_6380.conf
vi dump_6380.rdb
dbfilename dump_6380.rdb
appendfilename "appendonly_6380.aof"

# redis_6381.conf
vi dump_6381.rdb
dbfilename dump_6381.rdb
appendfilename "appendonly_6381.aof"

https://ithelp.ithome.com.tw/upload/images/20211006/20111658RCSgRzY8kU.png

https://ithelp.ithome.com.tw/upload/images/20211006/20111658CXfLJomSQl.png

https://ithelp.ithome.com.tw/upload/images/20211006/20111658JYtlHCRQMz.png

# 啟動服務
./redis-server ./redis_6379.conf 
./redis-server ./redis_6380.conf 
./redis-server ./redis_6381.conf 

#連線到 127.0.0.1 Port: 6379
./redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:1363014961ee0ae4061c2493c44a709b387c0c5e
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

#連線到 127.0.0.1 Port: 6380
./redis-cli -h 127.0.0.1 -p 6380
127.0.0.1:6380> info replication
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:7b48682e1628f579bf5a3c79420e0324c859046e
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

#連線到 127.0.0.1 Port: 6381
./redis-cli -h 127.0.0.1 -p 6381
127.0.0.1:6381> info replication
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:bf1bb9f71dd3b8a4fc2784f71c700ffbb5b9c800
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

https://ithelp.ithome.com.tw/upload/images/20211006/20111658ba2J6tMMoQ.png
https://ithelp.ithome.com.tw/upload/images/20211006/201116584LEZdMxLB1.png
https://ithelp.ithome.com.tw/upload/images/20211006/20111658yX0LiNbVom.png

# 將127.0.0.1 Port: 6380 與 127.0.0.1 Port: 6381 設為Replica
# 如果有設定replica password 則需要在replicaof 127.0.0.1 6379 -a password
./redis-cli -h 127.0.0.1 -p 6380
127.0.0.1:6380> replicaof 127.0.0.1 6379
OK
127.0.0.1:6380> role
1) "slave"
2) "127.0.0.1"
3) (integer) 6379
4) "connected"
5) (integer) 84

./redis-cli -h 127.0.0.1 -p 6381
127.0.0.1:6381> replicaof 127.0.0.1 6379
OK
127.0.0.1:6381> role
1) "slave"
2) "127.0.0.1"
3) (integer) 6379
4) "connected"
5) (integer) 0

# 確認Replication結果
./redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> role
1) "master"
2) (integer) 308
3) 1) 1) "127.0.0.1"
      2) "6381"
      3) "308"
   2) 1) "127.0.0.1"
      2) "6380"
      3) "308"
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6381,state=online,offset=336,lag=1
slave1:ip=127.0.0.1,port=6380,state=online,offset=336,lag=1
master_failover_state:no-failover
master_replid:a30878092cdd31e404af76eaa7a2d024c8ae843e
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:336
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:336

# 如果Replica要退出則可以透過以下方式
./redis-cli -h 127.0.0.1 -p 6381
127.0.0.1:6381> replicaof no one
# 在Master操作驗證同步到另外兩個Replica節點
./redis-cli -h 127.0.0.1 -p 6379

127.0.0.1:6379> set book a
OK
127.0.0.1:6379> get book
"a"
127.0.0.1:6379> set book b
OK
127.0.0.1:6379> get book
"b"

# 確認Replica
./redis-cli -h 127.0.0.1 -p 6380
127.0.0.1:6380> get book
"b"

# 確認Replica
./redis-cli -h 127.0.0.1 -p 6381
127.0.0.1:6381> get book
"b"

# 嘗試寫入Replica會有錯誤
127.0.0.1:6381> set book c
#(error) READONLY You can't write against a read only replica.

# 以上錯誤是因為這個設定而來的,當然也可以關閉但不建議
127.0.0.1:6381> config get replica-read-only
1) "replica-read-only"
2) "yes"

https://ithelp.ithome.com.tw/upload/images/20211006/20111658rhRD1aYJXh.png


上一篇
Day20 Redis架構實戰-持久化RDB+AOF
下一篇
Day22 Redis架構實戰-高可用性使用Sentinel機制
系列文
Redis還在學30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言